<?php
require('../include/init.php');
require('check_login.php');
$act = $_REQUEST['act'];
if($act == 'add') {
	$cate_name = trim($_POST['cate_name']);
	if(!empty($cate_name)) {
		$GLOBALS['db']->query("insert into kyz_products_cate(cate_name) values('$cate_name')");	
		header("Location: $_SERVER[PHP_SELF]");
	}
} elseif ($act == 'edit') {
	$cate_id = $_GET['cate_id'];
	$categorys = $GLOBALS['db']->getRow("select * from kyz_products_cate where cate_id='$cate_id' limit 1");
	$smarty->assign('show_edit_form', true);
	$smarty->assign('categorys', $categorys);
} elseif ($act == 'save') {
	$cate_id = $_POST['cate_id'];
	$cate_name = trim($_POST['cate_name']);
	if(!empty($cate_name)) {
		$GLOBALS['db']->query("update kyz_products_cate set cate_name='$cate_name' where cate_id='$cate_id'");
		header("Location: $_SERVER[PHP_SELF]");
	}
} elseif ($act == 'del') {
	$cate_id = $_GET['cate_id'];
	$num = $GLOBALS['db']->getOne("select count(*) from kyz_products where cate_id='$cate_id'");
	if($num > 0) {
		echo 0;exit();
	}
	$GLOBALS['db']->query("delete from kyz_products_cate where cate_id='$cate_id'");
	echo 1;exit();
} elseif ($act == 'checkExist') {
	$cate_name = $_GET['cate_name'];
	$o_cate_name = preg_replace('/\s+/', '', $_GET['o_cate_name']);
	if($cate_name != $o_cate_name) {
		$num = $GLOBALS['db']->getOne("select count(*) from kyz_products_cate where cate_name='$cate_name' limit 1");
		if($num > 0) {
			echo 'false';
		} else {
			echo 'true';
		}
	} else {
		echo 'true';
	}
	exit();
} else {
	if($act == 'show_add_form') {
		$smarty->assign('show_add_form', true);	
	}
	$cate_list = get_product_category();
	$smarty->assign('cate_list', $cate_list);
}
$smarty->display('admin/product_cate_manage.html');




?>
